package com.jurenzhao.partssystem.accessories.analysis.controller;


import com.jurenzhao.partssystem.accessories.analysis.entity.Piece_Info;
import com.jurenzhao.partssystem.accessories.analysis.service.Piece_InfoMapperSerevice;
import com.jurenzhao.partssystem.system.system_user.entity.System_Users;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/accessories")
public class Piece_InfoContrller {

    @Resource
    Piece_InfoMapperSerevice piece_infoMapperSerevice;

    @RequestMapping("/list")
    public String list(Model model, HttpSession httpSession) {
        System_Users systemUsers = (System_Users) httpSession.getAttribute("system_users");
        String rolename = systemUsers.getUserRoleString();
        System.out.println("--------------"+rolename);
        model.addAttribute("rolename",rolename);
        List<Piece_Info> list = piece_infoMapperSerevice.findAll();
        model.addAttribute("list", list);
        return "/accessories/pirece_info/list";
    }

    @RequestMapping("/delete/{pieceId}")
    public String delete(@PathVariable("pieceId")int pieceId){
        piece_infoMapperSerevice.delete(pieceId);
        return "redirect:/accessories/list";

    }
    @RequestMapping("/preAdd")
    public String  preAdd(Model model){
        return "/accessories/pirece_info/add";
    }
    @RequestMapping("/add")
    public  String add(Piece_Info piece_info){
        piece_infoMapperSerevice.save(piece_info);
        return "redirect:/accessories/list";

    }
    @RequestMapping("/preUpdate/{pieceId}")
    public String  preUpdate(Model model ,@PathVariable("pieceId") int pieceId) {
        Piece_Info piece_info = piece_infoMapperSerevice.findById(pieceId);
        model.addAttribute("typeList", piece_info);
        return"/accessories/pirece_info/update";
    }
    @RequestMapping("/update")
    public  String update(Piece_Info piece_info){
        piece_infoMapperSerevice.update(piece_info);
        return "redirect:/accessories/list";
    }

}
